package com.wyy.web;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.wyy.pojo.Articles;
import com.wyy.pojo.LikeArticle;
import com.wyy.pojo.model.ArticleQueryCriteria;
import com.wyy.pojo.model.LabelSearch;
import com.wyy.response.CommonReturnType;
import com.wyy.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @program: gytcb
 * @author: 吴磊
 * @create: 2021-06-03 20:23
 */
@RestController
public class ArticleAction {
    @Autowired
    private ArticleService articleService;

    private static final String GET_PROVIDER_KEY = "gytcb-article";

    /**
     * 获取热门的文章标签
     *
     * @return
     */
    @RequestMapping(value = "/getHotArticleLabel", method = RequestMethod.POST)
    @HystrixCommand(fallbackMethod = "hystrixFallbackMethod")
    public CommonReturnType getHotArticleLabel() {
        return CommonReturnType.create(articleService.getHotArticleLabel());

    }

    public CommonReturnType hystrixFallbackMethod() {
        return CommonReturnType.create("服务熔断");
    }

    /**
     * 根据内容模糊查询文章
     *
     * @param articles
     * @return
     */

    @RequestMapping(value = "/getArticleByArticleContentLikeSearch", method = RequestMethod.POST)
    @SentinelResource(value = GET_PROVIDER_KEY, blockHandler = "blockHandlerMethod1")
    public CommonReturnType getArticleByArticleContentLikeSearch(@RequestBody Articles articles) {
        Map<String, Object> article = articleService.getArticleByArticleContentLikeSearch(articles);
        return CommonReturnType.create(article);
    }

    public CommonReturnType blockHandlerMethod1(Articles articles, BlockException e) {
        return CommonReturnType.create("服务限流");
    }


    /**
     * 根据文章Id查询文章
     *
     * @param articles
     * @return
     */
    @SentinelResource(value = GET_PROVIDER_KEY, blockHandler = "blockHandlerMethod1")
    @RequestMapping(value = "/getArticleByArticleId", method = RequestMethod.POST)
    public CommonReturnType getArticleByArticleId(@RequestBody Articles articles) {
        Map<String, Object> map = articleService.getArticleByArticleId(articles);
        return CommonReturnType.create(map);
    }

    public CommonReturnType hystrixGetArticleByArticleId(Articles articles, BlockException e) {
        return CommonReturnType.create("服务限流");
    }


    /**
     * 用户发布文章
     *
     * @param articles
     * @return
     */
    @SentinelResource(value = GET_PROVIDER_KEY, blockHandler = "blockHandlerMethod1")
    @RequestMapping(value = "/insertArticle", method = RequestMethod.POST)
    public CommonReturnType tokenInsertArticle(@RequestBody Articles articles) {
        articleService.insertArticle(articles);
        return CommonReturnType.create(null);
    }

    /**
     * 通过文章Id删除文章及其一切相关
     *
     * @param articleId
     * @return
     */
    @RequestMapping(value = "/deleteArticleByArticleId", method = RequestMethod.POST)
    public CommonReturnType tokenDeleteArticleByArticleId(@RequestBody String articleId) {
        articleService.deleteArticleByArticleId(articleId);
        return CommonReturnType.create(null);
    }

    /**
     * 编辑帖子
     *
     * @param articles
     * @return
     */
    @SentinelResource(value = GET_PROVIDER_KEY, blockHandler = "blockHandlerMethod1")
    @RequestMapping(value = "/updateArticle", method = RequestMethod.POST)
    public CommonReturnType tokenUpdateArticle(@RequestBody Articles articles) {
        articleService.updateArticle(articles);
        return CommonReturnType.create(null);
    }

    /**
     * 点赞或取消点赞
     *
     * @param likeArticle
     * @return
     */
    @RequestMapping(value = "/likeArticle", method = RequestMethod.POST)
    public CommonReturnType tokenLikeOrCancelLikeArticle(@RequestBody LikeArticle likeArticle) {
        articleService.likeOrCancelLikeArticle(likeArticle);
        return CommonReturnType.create(null);
    }

    /**
     * 通过一些条件获取帖子
     *
     * @return
     */
    @RequestMapping(value = "/getArticleBySomething", method = RequestMethod.POST)
    public CommonReturnType getArticleBySomething(@RequestBody ArticleQueryCriteria articleQueryCriteria) {
        Map<String, Object> map = articleService.getArticleBySomething(articleQueryCriteria);
        return CommonReturnType.create(map);
    }

    /**
     * 通过文章标签模糊查询文章
     *
     * @param labelSearch
     * @return
     */
    @RequestMapping(value = "/getArticleBySearchLabel", method = RequestMethod.POST)
    public CommonReturnType getArticleBySearchLabel(@RequestBody LabelSearch labelSearch) {
        Map<String, Object> map = articleService.getArticleBySearchLabel(labelSearch);
        return CommonReturnType.create(map);
    }
}
